
package com.gzdh.tang.warehouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzdh.tang.comm.util.ObjectUtil;
import com.gzdh.tang.modu.dto.DInStockKuCunDto;
import com.gzdh.tang.modu.dto.InventoryNumberDto;
import com.gzdh.tang.modu.entity.TangDevice;
import com.gzdh.tang.modu.mapper.MPublicMapper;
import com.gzdh.tang.modu.vo.InStockTempVo;
import com.gzdh.tang.warehouse.entity.InStock;
import com.gzdh.tang.warehouse.mapper.InStockMapper;
import com.gzdh.tang.warehouse.service.InStockService;
import groovy.util.logging.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("inStockService")
@Transactional
public class InStockServiceImpl extends ServiceImpl<InStockMapper, InStock> implements InStockService {


    @Autowired
    InStockMapper inStockMapper;

    @Autowired
    MPublicMapper publicMapper;

    @Override
    public Long saveInStock(InStockTempVo inStockTempVoTemp) {
        return inStockMapper.insertInStock(inStockTempVoTemp);
    }

    @Override
    public Map<String, Object> queryInventoryByDeviceId(InventoryNumberDto inventoryNumberDto) {
        return inStockMapper.queryInventoryByDeviceId(inventoryNumberDto);
    }

    @Override
    public void insertInStock(List<InStockTempVo> inStockTempVos, TangDevice tangDevice) {
        List<DInStockKuCunDto> dInStockKuCunDtos = publicMapper.getDInStockKuCun(tangDevice.getAddr(), tangDevice.getCkId());
        for (InStockTempVo goo : inStockTempVos) {
            DInStockKuCunDto dto = dInStockKuCunDtos.stream().filter(dInStockKuCunDto -> dInStockKuCunDto.getDeviceid()
                    .equals(goo.getDeviceid())).findFirst().orElse(null);
            if (dto != null) {
                goo.setPriceVer(dto.getPriceVer());
                goo.setPrice(new BigDecimal(dto.getPrice()).floatValue());
                goo.setSalePrice(new BigDecimal(dto.getSalePrice()).floatValue());
            }
            Long returnId = inStockMapper.insertInStock(goo);
            if (returnId > 0) {
                goo.setId(returnId);
            }
        }
    }

    @Override
    public List<DInStockKuCunDto> getDeviceInventory(String deviceId, Integer addr, Integer ckId) {
        QueryWrapper<InStock> queryWrapper = new QueryWrapper<InStock>()
                .select("deviceId,price,sum(number) number,price_ver priceVer,flag," +
                        "max(price_adjust_times) priceAdjustTimes,max(sale_price) salePrice")
                .eq("deviceId", deviceId)
                .eq("addr", addr)
                .eq("ck_id", ckId)
                .groupBy("deviceId,price,price_ver,flag");
        List<Map<String, Object>> inventoryList = inStockMapper.selectMaps(queryWrapper);
        Map<String, List<Map<String,Object>>> currentMap = new HashMap<>();
        for (Map<String, Object> map : inventoryList) {
            String key = ObjectUtil.getStr(map.get("priceVer"));
            if(!currentMap.containsKey(key)) {
                currentMap.put(key, new LinkedList<>());
            }
            List<Map<String, Object>> list = currentMap.get(key);
            list.add(map);
            currentMap.put(key, list);
        }
        List<DInStockKuCunDto> inStockKuCunList = new LinkedList<>();
        for (List<Map<String, Object>> list : currentMap.values()) {
            DInStockKuCunDto dto = new DInStockKuCunDto();
            dto.setDeviceid(ObjectUtil.getStr(list.get(0).get("deviceId")));
            dto.setPrice(ObjectUtil.getStr(list.get(0).get("price")));
            dto.setSalePrice(ObjectUtil.getStr(list.get(0).get("salePrice")));
            dto.setPriceAdjustTimes(ObjectUtil.getStr(list.get(0).get("priceAdjustTimes")));
            dto.setPriceVer(ObjectUtil.getInt(list.get(0).get("priceVer")));
            BigDecimal totalNumber = new BigDecimal(0);
            for (Map<String, Object> map : list) {
                BigDecimal number = ObjectUtil.getBigDecimal(map.get("number"));
                switch (ObjectUtil.getStr(map.get("flag")).toUpperCase()) {
                    case "JH":
                    case "PH":
                        totalNumber = totalNumber.add(number);
                        break;
                    case "CH":
                        totalNumber = totalNumber.subtract(number);
                        break;
                }
            }
            dto.setNumber(totalNumber.setScale(2, BigDecimal.ROUND_DOWN).toPlainString());
            if(totalNumber.compareTo(new BigDecimal(0)) != 0) {
                inStockKuCunList.add(dto);
            }
        }
        return inStockKuCunList;
    }
}